﻿#region

using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using System.Text;
using Newtonsoft.Json;
using WebIG.Data;

#endregion

namespace WebIG.Service
{
    public class IGEndpoint : IEndpoint
    {
        public AuthResult RequestAccessToken(string code)
        {
            var postData = new NameValueCollection()
            {
                {"client_id", InstagramAPI.ClientID},
                {"client_secret", InstagramAPI.ClientSecret},
                {"grant_type", "authorization_code"},
                {"redirect_uri", InstagramAPI.RedirectUri},
                {"code", code}
            };

            var data = Utility.HttpPost("https://api.instagram.com/oauth/access_token", postData);

            var authResult = JsonConvert.DeserializeObject<AuthResult>(data);

            return authResult;
        }

        public List<Feed> GetUserMedia(string id, string accessToken)
        {
            var data = Utility.HttpGet(InstagramAPI.Uri_Users_UserId_Media_Recent, id, accessToken);

            var jsondata = JsonConvert.DeserializeObject<ResponseData<List<Feed>>>(data);

            return jsondata.Data;
        }

        public List<Feed> GetSelfFeeds(string accessToken)
        {
            var data = Utility.HttpGet(InstagramAPI.Uri_Users_Self_Feed, accessToken);

            var jsondata = JsonConvert.DeserializeObject<ResponseData<List<Feed>>>(data);

            return jsondata.Data;
        }

        public List<Feed> GetMediaPopular()
        {
            var url = string.Format("https://api.instagram.com/v1/media/popular?client_id={0}", InstagramAPI.ClientID);
            var data = Utility.HttpGet(url);

            var jsondata = JsonConvert.DeserializeObject<ResponseData<List<Feed>>>(data);

            return jsondata.Data;
        }

        public Feed GetMedia(string id, string accessToken)
        {
            var data = Utility.HttpGet(InstagramAPI.Uri_Media_MediaId, id, accessToken);

            var jsondata = JsonConvert.DeserializeObject<ResponseData<Feed>>(data);

            return jsondata.Data;
        }

        public User GetUser(string id, string accessToken)
        {
            var data = Utility.HttpGet(InstagramAPI.Uri_Users_UserId, id, accessToken);

            var jsondata = JsonConvert.DeserializeObject<ResponseData<User>>(data);

            return jsondata.Data;
        }

        public List<Feed> LoadMoreFeeds(int type, string userId, string maxId, string accessToken)
        {
            if (type == 0)
            {
                var data = Utility.HttpGet(InstagramAPI.Uri_Users_Self_Feed + string.Format("&max_id={0}", maxId), accessToken);

                var jsondata = JsonConvert.DeserializeObject<ResponseData<List<Feed>>>(data);

                return jsondata.Data;
            }
            else if (type == 1)
            {
                var data = Utility.HttpGet(InstagramAPI.Uri_Users_UserId_Media_Recent + string.Format("&max_id={0}", maxId), userId, accessToken);

                var jsondata = JsonConvert.DeserializeObject<ResponseData<List<Feed>>>(data);

                return jsondata.Data;
            }

            return new List<Feed>();
        }

        public List<User> GetMediaLikes(string mediaId, string accessToken)
        {
            var data = Utility.HttpGet(InstagramAPI.Uri_Media_Likes_All, mediaId, accessToken);

            var jsondata = JsonConvert.DeserializeObject<ResponseData<List<User>>>(data);

            return jsondata.Data;
        }

        public ResponseData<string> SetMediaLike(string mediaId, string accessToken)
        {
            var postData = new NameValueCollection()
            {
                {"access_token", accessToken},
            };

            var data = Utility.HttpPost(InstagramAPI.Uri_Media_Likes_Set, postData, mediaId);

            var response = JsonConvert.DeserializeObject<ResponseData<string>>(data);

            return response;
        }

        public ResponseData<string> DelMediaLike(string mediaId, string accessToken)
        {
            var data = Utility.HttpRequest(InstagramAPI.Uri_Media_Likes_Del, HttpMethod.DELETE, mediaId, accessToken);

            var response = JsonConvert.DeserializeObject<ResponseData<string>>(data);

            return response;
        }

        public ResponseData<Relationship> GetRelationship(string userId, string accessToken)
        {
            var data = Utility.HttpGet(InstagramAPI.Uri_Relationship, userId, accessToken);

            var response = JsonConvert.DeserializeObject<ResponseData<Relationship>>(data);

            return response;
        }

        public ResponseData<Relationship> ModifyRelationship(string userId, string accessToken, string action)
        {
            var data = Utility.HttpPost(InstagramAPI.Uri_Relationship, new NameValueCollection { { "action", action } }, userId, accessToken);

            var response = JsonConvert.DeserializeObject<ResponseData<Relationship>>(data);

            return response;
        }
    }
}